#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <string.h>
#include "mini.kit/socket.h"

int main(int argc, char* argv[])
{
	int32_t port;
	if( argc < 3 ) {
		fprintf(stderr, "usage: %s host port\n", argv[0]);
		return 1;
	}
	port = atoi( argv[2] );
	try{
		MINIKIT::UDPServerSocket srv;
		srv.bind( argv[1], port );
		fprintf(stdout, "bind[%s:%d]\n", srv.getLocalAddr().c_str(),  srv.getLocalPort() );
		do{
			sockaddr  sa;
			socklen_t sa_len = sizeof(sa);
			char buff[256] = {0};
			srv.recvFrom( buff, 256, &sa, &sa_len ); 
			fprintf(stdout, "[%s:%d <- %s:%d] recv: %s\n"
				, srv.getLocalAddr().c_str(),  srv.getLocalPort()
				, MINIKIT::Socket::toAddr(sa).c_str(),  MINIKIT::Socket::toPort(sa)
				, buff
			);
			srv.sendTo( buff, ::strlen(buff), &sa, sa_len );
		}while(1);
	}
	catch(MINIKIT::SocketException& ex)
	{
		fprintf(stderr, "err: %s\n", ex.what());
	}
	
	return 0;
}

